<!DOCTYPE html>
<html>
<head>
    <title>任务编辑</title>
    {% include 'system/common/header.html' %}
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">ID标识</label>
                    <div class="layui-input-block">
                        <input type="text" name="id" value="{{ task.id }}" readonly class="layui-input layui-disabled">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="title" autocomplete="off" placeholder="请输入名称"
                               value="{{ task.name }}" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">任务类型</label>
                    <div class="layui-input-block">
                        <select name="type" lay-filter="type" lay-verify="required" lay-search>
                            <option value="date" {% if task.trigger == 'date' %}selected{% endif %}>date 一次性指定固定时间，只执行一次</option>
                            <option value="interval" {% if task.trigger == 'interval' %}selected{% endif %}>interval 间隔调度，隔多长时间执行一次</option>
                            <option value="cron" {% if task.trigger == 'cron' %}selected{% endif %}>cron 指定相对时间执行</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">函数</label>
                    <div class="layui-input-block">
                        <select name="functions" lay-verify="required" lay-search>
                            {% for func_name, func_info in task_list.items() %}
                                <option value="{{ func_name }}" {% if task.func == func_name %}selected{% endif %}>{{ func_info.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item" id="datetimeItem" {% if task.trigger != 'date' %}style="display: none"{% endif %}>
                    <label class="layui-form-label">固定时间</label>
                    <div class="layui-input-block">
                        <input name="datetime" type="text" class="layui-input" id="datetime" 
                               value="{% if task.start_date %}{{ task.start_date.strftime('%Y-%m-%d %H:%M:%S') }}{% endif %}">
                    </div>
                </div>
                <div class="layui-form-item" id="timeItem" {% if task.trigger == 'date' %}style="display: none"{% endif %}>
                    <label class="layui-form-label">时间参数</label>
                    <div class="layui-input-block">
                        <input name="time" type="text" class="layui-input" id="time"
                               value="{{ task.trigger_args or '' }}"
                               placeholder="例如: {'hours': 1} 或 {'hour': 2}">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="enabled" lay-skin="switch" lay-text="启用|禁用"
                               {% if task.enabled %}checked{% endif %}>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="task-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'system/common/footer.html' %}
<script>
    layui.use(['form', 'jquery', 'laydate'], function () {
        let form = layui.form
        let $ = layui.jquery
        let laydate = layui.laydate;

        // 初始化日期时间选择器
        laydate.render({
            elem: '#datetime',
            type: 'datetime'
        });

        // 任务类型切换
        form.on('select(type)', function (data) {
            if (data.value === 'date') {
                $('#timeItem').hide()
                $('#datetimeItem').show()
            } else {
                $('#datetimeItem').hide()
                $('#timeItem').show()
                if (data.value === 'interval') {
                    $('#time').attr('placeholder', '例如: {"hours": 1, "minutes": 30}')
                } else if (data.value === 'cron') {
                    $('#time').attr('placeholder', '例如: {"hour": 2, "minute": 30}')
                }
            }
        })

        // 表单提交
        form.on('submit(task-save)', function (data) {
            let formData = data.field;

            // 处理开关状态
            formData.enabled = formData.enabled === 'on';

            // 验证时间参数
            if (formData.type !== 'date' && formData.time) {
                try {
                    JSON.parse(formData.time);
                } catch (e) {
                    layer.msg('时间参数格式错误，请输入有效的JSON格式', {icon: 2, time: 2000});
                    return false;
                }
            }

            $.ajax({
                url: '/admin/sys/task/update/{{ task.id }}',
                data: JSON.stringify(formData),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name))
                            parent.layui.table.reload('task-table')
                        })
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 2000})
                    }
                },
                error: function() {
                    layer.msg('请求失败，请检查网络连接', {icon: 2, time: 2000})
                }
            })
            return false
        })
    })
</script>
</body>
</html> 